﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  Orders.aspx.cs
 作    者:范徐超
 编写日期:2009-10-12
 审    核:
 审核日期:
 描    述:订餐上报信息管理页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;
using Cisoft.IntegrationSystem.Common;

public partial class Business_Orders : WebUI.Page.ManagePage
{
    private RuleBO ruleBO = new RuleBO();
    private RuleMO ruleMO = new RuleMO();
    private ShortMessageBO smBO = new ShortMessageBO();
    private ShortMessageMO smMO = new ShortMessageMO();
    private SetBO sBO = new SetBO();
    private SetMO sMO = new SetMO();
    private OrderBO oBO = new OrderBO();
    private OrderMO oMO = new OrderMO();

    private int pageCount = -1;

    private DataTable myDataTable = new DataTable();

    #region 页面加载事件
    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindHour();
            //Bind();

            txtDateA.Text = DateTime.Now.ToShortDateString();
            txtDateB.Text = DateTime.Now.ToShortDateString();
            ddl_StartHour.SelectedIndex = 0;
            ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
            string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
            string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
            DataSet ds = oBO.GetList(" OrderTime between '" + startTime + "' and '" + endTime + "' ");
            Cache["List"] = ds.Tables[0];
            Bind(Cache["List"] as DataTable, gvitem);
        }
    }
    #endregion

    #region 显示数据 private void GetList()
    /// <summary>
    /// 显示数据
    /// </summary>
    private DataTable GetList()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        #region 将数据存放在缓存中
        //if (Cache["List"] == null)
        //{
        //    //ds = ruleBO.GetAllList();
        //    string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
        //    string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
        //    ds = oBO.GetList(" OrderTime between '" + startTime + "' and '" + endTime + "' ");
        //    //if (ds.Tables[0].Rows.Count > 0)
        //    //{
        //        Cache.Insert("List", ds.Tables[0], null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));
        //    //}
        //}
        //else
        //{
        //    dt = (DataTable)Cache["List"];
        //    //ds.Tables[0] = dt;
        //}
        #endregion

        string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
        string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
        ds = oBO.GetList(" OrderTime between '" + startTime + "' and '" + endTime + "' ");
        dt = ds.Tables[0];
        return dt;
    }
    #endregion

    #region 翻页事件
    /// <summary>
    /// 翻页事件
    /// </summary>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        PagerEvent(sender, e, gvitem);
        
        //Bind();
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void butlookout_Click(object sender, ImageClickEventArgs e)
    {
        //查询条件
        string condition = txtPropasal.Text.Trim();

        //判断开始时间是否大于结束时间
        if(IsValid)
        {
            if (!string.IsNullOrEmpty(txtDateA.Text) && !string.IsNullOrEmpty(txtDateB.Text))
            {
                DateTime dtforend = Convert.ToDateTime(txtDateB.Text + " " + ddl_EndHour.SelectedValue + ":00");
                DateTime dtforstart = Convert.ToDateTime(txtDateA.Text + " " +  ddl_StartHour.SelectedValue + ":00");
                
                if (dtforend <= dtforstart)
                {
                    lbldate.Text = "<span style='font-weight:bold; color:red;'>结束时间必须大于开始时间！</span>";
                    return;
                }
            }
            string startTime = string.IsNullOrEmpty(txtDateA.Text) ? "" : txtDateA.Text + " " + ddl_StartHour.SelectedValue + ":00";
            string endTime = string.IsNullOrEmpty(txtDateB.Text) ? "" : txtDateB.Text + " " + ddl_EndHour.SelectedValue + ":00";
            string txtField = string.IsNullOrEmpty(txtPropasal.Text) ? "" : txtPropasal.Text;

            DataSet ds = oBO.GetQueryList(startTime, endTime, txtField);
            Cache["List"] = ds.Tables[0];
            Bind(Cache["List"] as DataTable, gvitem);
        }
    }
    #endregion

    #region 数据绑定并分页
    /// <summary>
    /// 数据绑定并分页
    /// </summary>
    /// <param name="dt">数据源DataTable</param>
    /// <param name="gv">绑定的控件GridView</param>
    private void Bind(DataTable dt, GridView gv)
    {

        pageCount = dt.Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            Cache["orderpagenum"] = pageCount;
            DataView dv = dt.DefaultView;
            dv.Sort = "OrderTime desc";
            gv.DataSource = dv.ToTable();
            gv.DataBind();
            div_pager.Visible = true;
            butState(gv, btnFirst, btnPrev, btnNext, btnLast);
            ShowStats(gv, lblCurrentIndex, lblNumbers, lblPageCount, "条订餐短信", Cache["orderpagenum"].ToString());
            CountDC(dt);
        }
        else
        {
            gv.DataSource = null;
            gv.DataBind();
            ViewState["numbers"] = "0";
            Cache["orderpagenum"] = 0;
            div_pager.Visible = false;
            butState(gv, btnFirst, btnPrev, btnNext, btnLast);
            ShowStats(gv, lblCurrentIndex, lblNumbers, lblPageCount, "条订餐短信", Cache["orderpagenum"].ToString());
            CountDC(dt);
        }
    }

    private void Bind()
    {
        if (GetList().Rows.Count > 0)
        {
            Bind(GetList(), gvitem);
        }
        else
        {
            gvitem.DataSource = null;
            gvitem.DataBind();
        }
    }
    #endregion

    #region gridview中的事件

    #region RowDataBound事件 protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标在列表行中停留时的样式
        for (int i = -1; i < gvitem.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                //Label Lbl_RuleReply = (Label)e.Row.FindControl("Lbl_RuleReply");
                //Lbl_RuleReply.Attributes.Add("onmousesover", "document.getElementById('div_reply').style.display='';");
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            
            //Label Lbl_ShortMessageSend = e.Row.FindControl("Lbl_ShortMessageSend") as Label;
            //短信内容
            Label Lbl_OrderContent = e.Row.FindControl("Lbl_OrderContent") as Label;
            string s = Lbl_OrderContent.Text;

            //用餐时间
            Label Lbl_UseTime = e.Row.FindControl("Lbl_UseTime") as Label;
            

            if (!string.IsNullOrEmpty(s))
            {
                if (s.Split('[').Length ==3)
                {
                    Lbl_UseTime.Text = s.Split('[')[2].TrimEnd(']');
                }
                else
                {
                    Lbl_UseTime.Text = s.Split('[')[1].Split(Convert.ToChar("为"))[1];
                }
                s = s.Split('[')[1].Split(']')[0];
            }
            Label Lbl_OrderStatus = e.Row.FindControl("Lbl_OrderStatus") as Label;
            if (Lbl_OrderStatus.Text.Trim() == "1")
            {
                Lbl_OrderContent.Text = "<span style='font-weight:bold; color:red;'>员工已取消预定 </span>" + (s.Length > 16 ?s.Substring(0, 14) + "..." : s);
                Lbl_OrderContent.ToolTip = s.ToString();
            }
            if (Lbl_OrderStatus.Text.Trim() == "0")
            {
                Lbl_OrderContent.Text = "<span style='font-weight:bold; color:red;'>已过期 </span>" + (s.Length > 16 ? s.Substring(0, 14) + "..." : s);
                Lbl_OrderContent.ToolTip = s.ToString();
            }
            if (Lbl_OrderStatus.Text.Trim() == "")
            {
                Lbl_OrderContent.Text = s.Length > 28 ? s.Substring(0, 26) + "..." : s;
                Lbl_OrderContent.ToolTip = s.ToString();
            }
            //短信发送时间
            Label Lbl_OrderTime = e.Row.FindControl("Lbl_OrderTime") as Label;
            Lbl_OrderTime.Text = Convert.ToDateTime(Lbl_OrderTime.Text).GetDateTimeFormats('f')[0].ToString();
            //短信发送类型
            Label Lbl_OrderType = e.Row.FindControl("Lbl_OrderType") as Label;
            if (Lbl_OrderType.Text == "DC")
            {
                Lbl_OrderType.Text = "订餐";
            }
            if(Lbl_OrderType.Text == "YD")
            {
                Lbl_OrderType.Text = "订包厢";
            }
            //短信发送人
            Label Lbl_OrderMan = e.Row.FindControl("Lbl_OrderMan") as Label;
            CustomersBO cBO = new CustomersBO();
            CustomersMO cMO = new CustomersMO();
            try
            {
                cMO = cBO.GetModel(Lbl_OrderMan.Text);
                if (cMO != null)
                {
                    Lbl_OrderMan.Text = cMO.CustomerName + "/" + cMO.CustomerMobile;
                }
            }
            catch
            { 
                
            }

            
        }

        //显示行号
        AddNo(gvitem, "lbl_No");

    }
    #endregion

    #region PageIndexChanging事件 protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    /// <summary>
    /// PageIndexChanging事件
    /// </summary>
    protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvitem.PageIndex = e.NewPageIndex;
    }
    #endregion

    #region 查看事件
    protected void gvitem_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Edit(sender, e);
    }
    #endregion

    #endregion

    #region 命令按钮事件

    #region 修改事件
    /// <summary>
    /// 修改事件
    /// </summary>
    private void Edit(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            string[] s = e.CommandArgument.ToString().Split(',');

            Response.Redirect("OrderDetail.aspx?OrderID=" + e.CommandArgument);
        }
        if (e.CommandName == "send")
        {
            Response.Redirect("OrderReply.aspx?Address=" + e.CommandArgument);
        }
    }
    #endregion

    #endregion

    #region 从字符串中转换日期格式 string GetDateTime(string input)
    /// <summary>
    /// 从字符串中转换日期格式
    /// </summary>
    /// <param name="input">日期字符串</param>
    private string GetDateTime(string input)
    {
        string yyyy = input.Substring(0, 4);
        string MM = input.Substring(4, 2);
        string dd = input.Substring(6, 2);
        string hh = input.Substring(9, 2);
        string mm = input.Substring(11, 2);

        string str = String.Format("{0}年{1}月{2}日 {3}:{4}", yyyy, MM, dd, hh, mm);

        return str;
    }
    #endregion

    #region 昨天的订餐信息
    protected void btn_LastDay_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTime.Now.AddDays(-1).ToShortDateString();
        txtDateB.Text = DateTime.Now.AddDays(-1).ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTime.Now.AddDays(-1).ToShortDateString() + " 00:00:00";
        string endTime = DateTime.Now.AddDays(-1).ToShortDateString() + " 23:59:59";
        DataSet ds = oBO.GetList(" OrderTime between '" + startTime + "' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 今天的订餐信息
    protected void btn_Today_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTime.Now.ToShortDateString();
        txtDateB.Text = DateTime.Now.ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
        string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
        DataSet ds = oBO.GetList(" OrderTime between '" + startTime + "' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 本周的订餐信息
    protected void btn_ThisWeekend_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.GetThisWeekendFirst();
        txtDateB.Text = DateTimeHelper.GetThisWeekendLast();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        DataSet ds = oBO.GetList(" OrderTime between '" + DateTimeHelper.GetThisWeekendFirst() + " 00:00:00' and '" + DateTimeHelper.GetThisWeekendLast() + " 23:59:59' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 上周的订餐信息
    protected void btn_LastWeekend_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.GetLastWeekendFirst();
        txtDateB.Text = DateTimeHelper.GetLastWeekendLast();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        DataSet ds = oBO.GetList(" OrderTime between '" + DateTimeHelper.GetLastWeekendFirst() + " 00:00:00' and '" + DateTimeHelper.GetLastWeekendLast() + " 23:59:59' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 本月的订餐信息 
    protected void btn_ThisMonth_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.FirstDayOfMonth().ToShortDateString();
        txtDateB.Text = DateTimeHelper.LastDayOfMonth().ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTimeHelper.FirstDayOfMonth().ToShortDateString() + " 00:00:00";
        string endTime = DateTimeHelper.LastDayOfMonth().ToShortDateString() + " 23:59:59";
        DataSet ds = oBO.GetList(" OrderTime between '" + startTime + " ' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 上月的订餐信息
    protected void btn_LastMonth_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.FirstDayOfPreviousMonth(DateTime.Now).ToShortDateString();
        txtDateB.Text = DateTimeHelper.LastDayOfPrdviousMonth(DateTime.Now).ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTimeHelper.FirstDayOfPreviousMonth(DateTime.Now).ToShortDateString() + " 00:00:00";
        string endTime = DateTimeHelper.LastDayOfPrdviousMonth(DateTime.Now).ToShortDateString() + " 23:59:59";
        DataSet ds = oBO.GetList(" OrderTime between '" + startTime + " ' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, gvitem);
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion
    #region 显示所有的订餐信息
    protected void btn_All_Click(object sender, EventArgs e)
    {
        txtDateA.Text = "";
        txtDateB.Text = "";
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = 0;
        DataSet ds = oBO.GetAllList();
        Cache["List"] = ds.Tables[0];
        Bind(Cache["List"] as DataTable, gvitem);
    }
    #endregion

    #region 统计订餐份数
    private void CountDC(DataTable dt)
    {
        int rowcount = 0;
        int cancel = 0;
        int over = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //所有
            //if (!string.IsNullOrEmpty(dt.Rows[i]["OrderCount"].ToString()))
            //{
            //    rowcount += int.Parse(dt.Rows[i]["OrderCount"].ToString());
            //}
            //if(!string.IsNullOrEmpty(dt.Rows[i]["OrderStatus"].ToString()))
            //{
            //    //已经取消
            //    if(dt.Rows[i]["OrderStatus"].ToString() == "1")
            //    {
                    
            //    }
            //}
            //if(!string.IsNullOrEmpty(
            if (!string.IsNullOrEmpty(dt.Rows[i]["OrderCount"].ToString()) && dt.Rows[i]["OrderStatus"].ToString() != "1" && dt.Rows[i]["OrderStatus"].ToString() == "0")
            {
                rowcount += int.Parse(dt.Rows[i]["OrderCount"].ToString());
            }
        }
        lbl_DCCount.Text = "总共有<span style='font-weight:bold;font-size:15px;'> " + rowcount + "  &nbsp;&nbsp;</span>份订餐";
    }
    #endregion

    #region 绑定小时
    private void BindHour()
    {
        for (int i = 0; i < 24; i++)
        {
            ListItem liforstart = new ListItem(i.ToString("00"), i.ToString("00"));
            ListItem liforend = new ListItem(i.ToString("00"), i.ToString("00"));

            ddl_StartHour.Items.Add(liforstart);
            ddl_EndHour.Items.Add(liforend);
        }
    }
    #endregion


    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Bind();
    }
}
